﻿// -----------------------------------------------------------------------
// <copyright file="NotificationTemplateParcer.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Notification.Common
{
	#region Usings

	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Xml;

	using MBOP.Core.Entities.Notification;

	#endregion

	/// <summary>
	/// Provides notification templates parsing.
	/// </summary>
	public static class NotificationTemplateParcer
	{
		#region Constants

		/// <summary>
		/// Represents the template tag name.
		/// </summary>
		private const string TAG_TEMPLATE = "template";

		/// <summary>
		/// Represents the parameter tag name.
		/// </summary>
		private const string TAG_PARAMETER = "parameter";

		/// <summary>
		/// Represents the enabled attribute name.
		/// </summary>
		private const string ATTRIBUTE_ENABLED = "enabled";

		/// <summary>
		/// Represents the notification type attribute name.
		/// </summary>
		private const string ATTRIBUTE_TYPE = "type";

		/// <summary>
		/// Represents the name attribute.
		/// </summary>
		private const string ATTRIBUTE_NAME = "name";

		#endregion

		/// <summary>
		/// Parses the file  matches the specified path.
		/// </summary>
		/// <param name="templatePath">The specified path.</param>
		/// <returns>Collection of the parsed templates.</returns>
		public static IEnumerable<NotificationTemplate> Parse(string templatePath)
		{
			List<NotificationTemplate> templates = new List<NotificationTemplate>();

			if (File.Exists(templatePath))
			{
				using (XmlReader reader = XmlReader.Create(templatePath, GetReaderCommonSettings()))
				{
					reader.ReadToFollowing(TAG_TEMPLATE);

					do
					{
						string isEnabledAsString = reader.GetAttribute(ATTRIBUTE_ENABLED);

						if (IsRightTemplate(isEnabledAsString))
						{
							NotificationTypes notificationType;

							if (!Enum.TryParse(reader.GetAttribute(ATTRIBUTE_TYPE), true, out notificationType))
							{
								continue;
							}

							NotificationTemplate currentTemplate = new NotificationTemplate();

							currentTemplate.Type = notificationType;

							Dictionary<string, string> parameters = new Dictionary<string, string>();

							reader.ReadToDescendant(TAG_PARAMETER);

							do
							{
								string name = reader.GetAttribute(ATTRIBUTE_NAME);

								if (!string.IsNullOrEmpty(name))
								{
									parameters.Add(name, reader.ReadElementContentAsString());
								}
							} 
							while (reader.ReadToNextSibling(TAG_PARAMETER));

							currentTemplate.Parameters = parameters;

							templates.Add(currentTemplate);
						}
					} 
					while (reader.ReadToFollowing(TAG_TEMPLATE));
				}
			}

			return templates;
		}

		#region Private Methods

		/// <summary>
		/// Checks that the current template is enabled or not.
		/// </summary>
		/// <param name="isEnabledAsString">The enable attribute in string representation.</param>
		/// <returns>The result of the checking.</returns>
		private static bool IsRightTemplate(string isEnabledAsString)
		{
			return !string.IsNullOrEmpty(isEnabledAsString) && isEnabledAsString != "false";
		}

		/// <summary>
		/// Gets the common xml reader settings.
		/// </summary>
		/// <returns>The common readers settings</returns>
		private static XmlReaderSettings GetReaderCommonSettings()
		{
			XmlReaderSettings settings = new XmlReaderSettings();

			settings.IgnoreComments = true;
			settings.CloseInput = true;

			return settings;
		}

		#endregion
	}
}
